<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <title>Nginx 内容缓存及常见参数配置 - 八月半的研发杂谈</title><meta name="Description" content="MidAug blog java docker hugo"><meta property="og:url" content="https://100180.xyz/posts/2021122101/">
  <meta property="og:site_name" content="八月半的研发杂谈">
  <meta property="og:title" content="Nginx 内容缓存及常见参数配置">
  <meta property="og:description" content="场景 使用场景:项目的页面需要加载很多数据，也不是经常变化的，不涉及个性化定制，为每次请求去动态生成数据，性能比不上根据请求路由和参数缓存一下">
  <meta property="og:locale" content="en">
  <meta property="og:type" content="article">
  <meta property="article:section" content="posts">
    <meta property="article:published_time" content="2021-12-21T18:11:33+00:00">
    <meta property="article:modified_time" content="2021-12-21T18:11:33+00:00">
    <meta property="article:tag" content="Nginx">
    <meta property="og:image" content="https://100180.xyz/images/whoami/avatar.jpg">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://100180.xyz/images/whoami/avatar.jpg"><meta name="twitter:title" content="Nginx 内容缓存及常见参数配置">
<meta name="twitter:description" content="场景 使用场景:项目的页面需要加载很多数据，也不是经常变化的，不涉及个性化定制，为每次请求去动态生成数据，性能比不上根据请求路由和参数缓存一下">
<meta name="application-name" content="MidAug&#39;s  Blog">
<meta name="apple-mobile-web-app-title" content="MidAug&#39;s  Blog"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="https://100180.xyz/posts/2021122101/" /><link rel="prev" href="https://100180.xyz/posts/2021111601/" /><link rel="next" href="https://100180.xyz/posts/2022083001/" /><link rel="stylesheet" href="/css/style.min.css"><link rel="preload" href="/lib/fontawesome-free/all.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
        <noscript><link rel="stylesheet" href="/lib/fontawesome-free/all.min.css"></noscript><link rel="preload" href="/lib/animate/animate.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
        <noscript><link rel="stylesheet" href="/lib/animate/animate.min.css"></noscript><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "Nginx 内容缓存及常见参数配置",
        "inLanguage": "en",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/100180.xyz\/posts\/2021122101\/"
        },"image": [{
                            "@type": "ImageObject",
                            "url": "https:\/\/100180.xyz\/images\/Apple-Devices-Preview.png",
                            "width":  3200 ,
                            "height":  2048 
                        }],"genre": "posts","keywords": "nginx","wordcount":  2522 ,
        "url": "https:\/\/100180.xyz\/posts\/2021122101\/","datePublished": "2021-12-21T18:11:33+00:00","dateModified": "2021-12-21T18:11:33+00:00","license": "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.","publisher": {
            "@type": "Organization",
            "name": "MidAug","logo": "https:\/\/100180.xyz\/images\/whoami\/avatar.jpg"},"author": {
                "@type": "Person",
                "name": "MidAug"
            },"description": ""
    }
    </script></head>
    <body data-header-desktop="fixed" data-header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="八月半的研发杂谈"><img
        class="lazyload logo"
        src="/svg/loading.min.svg"
        data-src="/favicon/apple-icon-60x60.png"
        data-srcset="/favicon/apple-icon-60x60.png, /favicon/apple-icon-60x60.png 1.5x, /favicon/apple-icon-60x60.png 2x"
        data-sizes="auto"
        alt="/favicon/apple-icon-60x60.png"
        title="/favicon/apple-icon-60x60.png" /><span id="id-1" class="typeit"></span></a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> Posts </a><a class="menu-item" href="/tags/"> Tags </a><a class="menu-item" href="/categories/"> Categories </a><a class="menu-item" href="/about/"> About </a><a class="menu-item" href="/disclaimer/"> Disclaimer </a><a class="menu-item" href="https://github.com/midaug" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw' aria-hidden='true'></i>  </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="Search titles or contents..." id="search-input-desktop">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-desktop" title="Search">
                            <i class="fas fa-search fa-fw" aria-hidden="true"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-desktop" title="Clear">
                            <i class="fas fa-times-circle fa-fw" aria-hidden="true"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin" aria-hidden="true"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="Switch Theme">
                    <i class="fas fa-adjust fa-fw" aria-hidden="true"></i>
                </a></div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="八月半的研发杂谈"><img
        class="lazyload logo"
        src="/svg/loading.min.svg"
        data-src="/favicon/apple-icon-60x60.png"
        data-srcset="/favicon/apple-icon-60x60.png, /favicon/apple-icon-60x60.png 1.5x, /favicon/apple-icon-60x60.png 2x"
        data-sizes="auto"
        alt="/favicon/apple-icon-60x60.png"
        title="/favicon/apple-icon-60x60.png" /><span id="id-2" class="typeit"></span></a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="Search titles or contents..." id="search-input-mobile">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-mobile" title="Search">
                            <i class="fas fa-search fa-fw" aria-hidden="true"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-mobile" title="Clear">
                            <i class="fas fa-times-circle fa-fw" aria-hidden="true"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin" aria-hidden="true"></i>
                        </span>
                    </div>
                    <a href="javascript:void(0);" class="search-cancel" id="search-cancel-mobile">
                        Cancel
                    </a>
                </div><a class="menu-item" href="/posts/" title="">Posts</a><a class="menu-item" href="/tags/" title="">Tags</a><a class="menu-item" href="/categories/" title="">Categories</a><a class="menu-item" href="/about/" title="">About</a><a class="menu-item" href="/disclaimer/" title="">Disclaimer</a><a class="menu-item" href="https://github.com/midaug" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw' aria-hidden='true'></i></a><a href="javascript:void(0);" class="menu-item theme-switch" title="Switch Theme">
                <i class="fas fa-adjust fa-fw" aria-hidden="true"></i>
            </a></div>
    </div>
</header><div class="search-dropdown desktop">
        <div id="search-dropdown-desktop"></div>
    </div>
    <div class="search-dropdown mobile">
        <div id="search-dropdown-mobile"></div>
    </div><main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">Contents</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animate__animated animate__flipInX">Nginx 内容缓存及常见参数配置</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel="author" class="author"><i class="fas fa-user-circle fa-fw" aria-hidden="true"></i>MidAug</a></span>&nbsp;<span class="post-category">included in <a href="/categories/nginx/"><i class="far fa-folder fa-fw" aria-hidden="true"></i>Nginx</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw" aria-hidden="true"></i>&nbsp;<time datetime="2021-12-21">2021-12-21</time>&nbsp;<i class="fas fa-pencil-alt fa-fw" aria-hidden="true"></i>&nbsp;2522 words&nbsp;
                <i class="far fa-clock fa-fw" aria-hidden="true"></i>&nbsp;6 minutes&nbsp;</div>
        </div><div class="details toc" id="toc-static"  data-kept="">
                <div class="details-summary toc-title">
                    <span>Contents</span>
                    <span><i class="details-icon fas fa-angle-right" aria-hidden="true"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#场景">场景</a>
      <ul>
        <li><a href="#基础基础">基础<a href="#%E5%9F%BA%E7%A1%80"></a></a></li>
        <li><a href="#nginx-缓存相关进程nginx-缓存相关进程">Nginx 缓存相关进程<a href="#nginx-%E7%BC%93%E5%AD%98%E7%9B%B8%E5%85%B3%E8%BF%9B%E7%A8%8B"></a></a></li>
        <li><a href="#其他常用参数其他常用参数">其他常用参数<a href="#%E5%85%B6%E4%BB%96%E5%B8%B8%E7%94%A8%E5%8F%82%E6%95%B0"></a></a></li>
        <li><a href="#清除缓存清除缓存">清除缓存<a href="#%E6%B8%85%E9%99%A4%E7%BC%93%E5%AD%98"></a></a></li>
      </ul>
    </li>
    <li><a href="#nginx-基于url参数的反向代理加缓存实践">Nginx 基于url参数的反向代理加缓存实践</a></li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><h2 id="场景">场景</h2>
<blockquote>
<p>使用场景:项目的页面需要加载很多数据，也不是经常变化的，不涉及个性化定制，为每次请求去动态生成数据，性能比不上根据请求路由和参数缓存一下结果，使用 Nginx 缓存将大幅度提升请求速度。</p>
</blockquote>
<h3 id="基础基础">基础<a href="#%e5%9f%ba%e7%a1%80" rel=""></a></h3>
<p>只需要配置 <code>proxy_cache_path</code> 和 <code>proxy_cache</code> 就可以开启内容缓存，前者用来设置缓存的路径和配置，后者用来启用缓存。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">http {
</span></span><span class="line"><span class="cl">    ...
</span></span><span class="line"><span class="cl">    proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    server {
</span></span><span class="line"><span class="cl">        proxy_cache mycache;
</span></span><span class="line"><span class="cl">        location / {
</span></span><span class="line"><span class="cl">            proxy_pass http://localhost:8000;
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">} 
</span></span></code></pre></td></tr></table>
</div>
</div><p><strong>对应参数说明:</strong></p>
<blockquote>
<p>1.用于缓存的本地磁盘目录是 /path/to/cache/</p>
</blockquote>
<blockquote>
<p>2.levels 在 /path/to/cache/ 设置了一个两级层次结构的目录。将大量的文件放置在单个目录中会导致文件访问缓慢，所以针对大多数部署，我们推荐使用两级目录层次结构。如果 levels 参数没有配置，则 NGINX 会将所有的文件放到同一个目录中。</p>
</blockquote>
<blockquote>
<p>3.keys_zone 设置一个共享内存区，该内存区用于存储缓存键和元数据，有些类似计时器的用途。将键的拷贝放入内存可以使 NGINX 在不检索磁盘的情况下快速决定一个请求是 <code>HIT</code> 还是 <code>MISS</code>，这样大大提高了检索速度。一个 1MB 的内存空间可以存储大约 8000 个 key，那么上面配置的 10MB 内存空间可以存储差不多 80000 个key。</p>
</blockquote>
<blockquote>
<p>4.max_size 设置了缓存的上限（在上面的例子中是 10G）。这是一个可选项；如果不指定具体值，那就是允许缓存不断增长，占用所有可用的磁盘空间。当缓存达到这个上线，处理器便调用 cache manager 来移除最近最少被使用的文件，这样把缓存的空间降低至这个限制之下。</p>
</blockquote>
<blockquote>
<p>5.inactive 指定了项目在不被访问的情况下能够在内存中保持的时间。在上面的例子中，如果一个文件在 60 分钟之内没有被请求，则缓存管理将会自动将其在内存中删除，不管该文件是否过期。该参数默认值为 10 分钟（10m）。注意，非活动内容有别于过期内容。NGINX 不会自动删除由缓存控制头部指定的过期内容（本例中Cache-Control:max-age=120）。过期内容只有在 inactive 指定时间内没有被访问的情况下才会被删除。如果过期内容被访问了，那么 NGINX 就会将其从原服务器上刷新，并更新对应的 inactive 计时器。</p>
</blockquote>
<blockquote>
<p>6.NGINX 最初会将注定写入缓存的文件先放入一个临时存储区域， use_temp_path=off 命令指示 NGINX 将在缓存这些文件时将它们写入同一个目录下。我们强烈建议你将参数设置为 off 来避免在文件系统中不必要的数据拷贝。use_temp_path 在 NGINX1.7 版本和 NGINX Plus R6 中有所介绍。</p>
</blockquote>
<blockquote>
<p>最终，proxy_cache 命令启动缓存那些 URL 与 location 部分匹配的内容（本例中，为<code>/</code>）。你同样可以将 proxy_cache 命令添加到 server 部分，这将会将缓存应用到所有的那些 location 中未指定自己的 proxy_cache 命令的服务中。</p>
</blockquote>
<h3 id="nginx-缓存相关进程nginx-缓存相关进程">Nginx 缓存相关进程<a href="#nginx-%e7%bc%93%e5%ad%98%e7%9b%b8%e5%85%b3%e8%bf%9b%e7%a8%8b" rel=""></a></h3>
<p>缓存中还涉及两个额外的NGINX进程：</p>
<ul>
<li><strong>cache manager</strong> 周期性地启动，检查高速缓存的状态。如果高速缓存大小超过 proxy_cache_path 中 max_size 参数设置的限制，则高速缓存管理器将删除最近访问过的数据。在两次缓存管理器启动的间隔，缓存的数据量可能短暂超过配置的大小。</li>
<li><strong>cache loader</strong> 只运行一次，NGINX 开始之后。它将有关以前缓存的数据的元数据加载到共享内存区域。一次加载整个缓存可能会消耗足够的资源来在启动后的最初几分钟内降低 NGINX 的性能。要避免这种情况，请通过在 proxy_cache_path 指令中包含以下参数来配置缓存的迭代加载：
<ul>
<li>loader_threshold - 迭代持续时间，以毫秒为单位（默认情况下 200）</li>
<li>loader_files - 一次迭代期间加载的最大项目数（默认情况下 100）</li>
<li>loader_sleeps - 迭代之间的延迟，以毫秒为单位（默认情况下 50）</li>
</ul>
</li>
</ul>
<p>在以下示例中，迭代持续数300 毫秒或直到 200 个项目被加载进去：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-gdscript3" data-lang="gdscript3"><span class="line"><span class="cl"><span class="n">proxy_cache_path</span> <span class="o">/</span><span class="n">data</span><span class="o">/</span><span class="n">nginx</span><span class="o">/</span><span class="n">cache</span> <span class="n">keys_zone</span><span class="o">=</span><span class="n">one</span><span class="p">:</span><span class="mi">10</span><span class="n">m</span> <span class="n">loader_threshold</span><span class="o">=</span><span class="mi">300</span> <span class="n">loader_files</span><span class="o">=</span><span class="mi">200</span><span class="p">;</span> 
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="其他常用参数其他常用参数">其他常用参数<a href="#%e5%85%b6%e4%bb%96%e5%b8%b8%e7%94%a8%e5%8f%82%e6%95%b0" rel=""></a></h3>
<p><strong>配置示例：</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">server {
</span></span><span class="line"><span class="cl">    ...
</span></span><span class="line"><span class="cl">    location / {
</span></span><span class="line"><span class="cl">        proxy_cache my_cache;
</span></span><span class="line"><span class="cl">        # proxy_cache_key &#34;$host$request_uri$cookie_user&#34;;
</span></span><span class="line"><span class="cl">        proxy_cache_min_uses 3;
</span></span><span class="line"><span class="cl">        proxy_cache_methods GET HEAD POST;
</span></span><span class="line"><span class="cl">        proxy_cache_valid 200 302 10m;
</span></span><span class="line"><span class="cl">        proxy_cache_valid 404      1m;
</span></span><span class="line"><span class="cl">        # proxy_cache_valid any 5m;
</span></span><span class="line"><span class="cl">        proxy_pass http://localhost:8000;
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">} 
</span></span></code></pre></td></tr></table>
</div>
</div><p><strong>对应参数说明：</strong>
<code>proxy_cache_key</code> 为更改计算密钥时使用的请求特征，指定缓存的 key，这个不推荐，示例是使用域名，请求url，用户 cookie 来当作 key，意味着一个页面将为不同的用户缓存 n 次，绝大多数情况不需要这样的操作。
<code>proxy_cache_min_uses</code> 为在缓存响应之前必须使用相同密钥的请求的最小次数。
<code>proxy_cache_methods</code> 为指定要被缓存的请求方式的响应值，默认为 GET 和 HEAD，新增其他的需要一起列出来，如上示例所示。
<code>proxy_cache_valid</code> 为响应状态码的缓存时间，示例可以为每个状态码缓存指定时间，也可以使用 any 进行全部状态码的缓存。</p>
<h3 id="清除缓存清除缓存">清除缓存<a href="#%e6%b8%85%e9%99%a4%e7%bc%93%e5%ad%98" rel=""></a></h3>
<p>需要提前加一个配置，用于标识使用 HTTP PURGE 方法的请求并删除匹配的 URL 对应的缓存。
1.在 <code>http {}</code> 上下文中创建新变量，例如 <code>$purge_method</code>, 他依赖于 <code>$request_method</code> 变量：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">http {
</span></span><span class="line"><span class="cl">    ...
</span></span><span class="line"><span class="cl">    map $request_method $purge_method {
</span></span><span class="line"><span class="cl">        PURGE 1;
</span></span><span class="line"><span class="cl">        default 0;
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">} 
</span></span></code></pre></td></tr></table>
</div>
</div><p>2.在 <code>location {}</code> 块中，已经配置缓存的前提下，引入 <code>proxy_cache_purge</code> 参数来指定清除缓存请求的条件。例如在上一步指定的 <code>$request_method</code></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">server {
</span></span><span class="line"><span class="cl">    listen      80;
</span></span><span class="line"><span class="cl">    server_name www.example.com;
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    location / {
</span></span><span class="line"><span class="cl">        proxy_pass  https://localhost:8002;
</span></span><span class="line"><span class="cl">        proxy_cache mycache;
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        proxy_cache_purge $purge_method;
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">} 
</span></span></code></pre></td></tr></table>
</div>
</div><p>配置完并使之生效之后，就可以发送一条 purge 请求来让缓存失效了，例如：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">curl -X PURGE -D – &#34;https://www.example.com/*&#34; 
</span></span></code></pre></td></tr></table>
</div>
</div><p>在该示例中，将清除具有公共 URL 部分（由星号通配符指定）的资源。但这些缓存条目不会从缓存中完全删除：它们会保留在磁盘上，直到它们被视为不活动（由<code>proxy_cache_path</code> 中的 <code>inactive</code>参数决定）的时候才完全删除，或缓存清除器（由 <code>proxy_cache_path</code> 中的 <code>purge</code> 决定），或客户端尝试访问它们的时候。</p>
<h2 id="nginx-基于url参数的反向代理加缓存实践">Nginx 基于url参数的反向代理加缓存实践</h2>
<p><strong>nginx.conf：</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">  1
</span><span class="lnt">  2
</span><span class="lnt">  3
</span><span class="lnt">  4
</span><span class="lnt">  5
</span><span class="lnt">  6
</span><span class="lnt">  7
</span><span class="lnt">  8
</span><span class="lnt">  9
</span><span class="lnt"> 10
</span><span class="lnt"> 11
</span><span class="lnt"> 12
</span><span class="lnt"> 13
</span><span class="lnt"> 14
</span><span class="lnt"> 15
</span><span class="lnt"> 16
</span><span class="lnt"> 17
</span><span class="lnt"> 18
</span><span class="lnt"> 19
</span><span class="lnt"> 20
</span><span class="lnt"> 21
</span><span class="lnt"> 22
</span><span class="lnt"> 23
</span><span class="lnt"> 24
</span><span class="lnt"> 25
</span><span class="lnt"> 26
</span><span class="lnt"> 27
</span><span class="lnt"> 28
</span><span class="lnt"> 29
</span><span class="lnt"> 30
</span><span class="lnt"> 31
</span><span class="lnt"> 32
</span><span class="lnt"> 33
</span><span class="lnt"> 34
</span><span class="lnt"> 35
</span><span class="lnt"> 36
</span><span class="lnt"> 37
</span><span class="lnt"> 38
</span><span class="lnt"> 39
</span><span class="lnt"> 40
</span><span class="lnt"> 41
</span><span class="lnt"> 42
</span><span class="lnt"> 43
</span><span class="lnt"> 44
</span><span class="lnt"> 45
</span><span class="lnt"> 46
</span><span class="lnt"> 47
</span><span class="lnt"> 48
</span><span class="lnt"> 49
</span><span class="lnt"> 50
</span><span class="lnt"> 51
</span><span class="lnt"> 52
</span><span class="lnt"> 53
</span><span class="lnt"> 54
</span><span class="lnt"> 55
</span><span class="lnt"> 56
</span><span class="lnt"> 57
</span><span class="lnt"> 58
</span><span class="lnt"> 59
</span><span class="lnt"> 60
</span><span class="lnt"> 61
</span><span class="lnt"> 62
</span><span class="lnt"> 63
</span><span class="lnt"> 64
</span><span class="lnt"> 65
</span><span class="lnt"> 66
</span><span class="lnt"> 67
</span><span class="lnt"> 68
</span><span class="lnt"> 69
</span><span class="lnt"> 70
</span><span class="lnt"> 71
</span><span class="lnt"> 72
</span><span class="lnt"> 73
</span><span class="lnt"> 74
</span><span class="lnt"> 75
</span><span class="lnt"> 76
</span><span class="lnt"> 77
</span><span class="lnt"> 78
</span><span class="lnt"> 79
</span><span class="lnt"> 80
</span><span class="lnt"> 81
</span><span class="lnt"> 82
</span><span class="lnt"> 83
</span><span class="lnt"> 84
</span><span class="lnt"> 85
</span><span class="lnt"> 86
</span><span class="lnt"> 87
</span><span class="lnt"> 88
</span><span class="lnt"> 89
</span><span class="lnt"> 90
</span><span class="lnt"> 91
</span><span class="lnt"> 92
</span><span class="lnt"> 93
</span><span class="lnt"> 94
</span><span class="lnt"> 95
</span><span class="lnt"> 96
</span><span class="lnt"> 97
</span><span class="lnt"> 98
</span><span class="lnt"> 99
</span><span class="lnt">100
</span><span class="lnt">101
</span><span class="lnt">102
</span><span class="lnt">103
</span><span class="lnt">104
</span><span class="lnt">105
</span><span class="lnt">106
</span><span class="lnt">107
</span><span class="lnt">108
</span><span class="lnt">109
</span><span class="lnt">110
</span><span class="lnt">111
</span><span class="lnt">112
</span><span class="lnt">113
</span><span class="lnt">114
</span><span class="lnt">115
</span><span class="lnt">116
</span><span class="lnt">117
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-gdscript3" data-lang="gdscript3"><span class="line"><span class="cl"><span class="n">user</span> <span class="n">nginx</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">worker_processes</span> <span class="n">auto</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">error_log</span> <span class="o">/</span><span class="k">var</span><span class="o">/</span><span class="nb">log</span><span class="o">/</span><span class="n">nginx</span><span class="o">/</span><span class="n">error</span><span class="o">.</span><span class="n">log</span> <span class="n">notice</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="n">pid</span> <span class="o">/</span><span class="k">var</span><span class="o">/</span><span class="n">run</span><span class="o">/</span><span class="n">nginx</span><span class="o">.</span><span class="n">pid</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">events</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">worker_connections</span> <span class="mi">1024</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">http</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">include</span> <span class="o">/</span><span class="n">etc</span><span class="o">/</span><span class="n">nginx</span><span class="o">/</span><span class="n">mime</span><span class="o">.</span><span class="n">types</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">default_type</span> <span class="n">application</span><span class="o">/</span><span class="n">octet</span><span class="o">-</span><span class="n">stream</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="n">log_format</span> <span class="n">main</span> <span class="s1">&#39;$remote_addr - $remote_user [$time_local] &#34;$request&#34; &#39;</span>
</span></span><span class="line"><span class="cl">    <span class="s1">&#39;$status $body_bytes_sent &#34;$http_referer&#34; &#39;</span>
</span></span><span class="line"><span class="cl">    <span class="s1">&#39;&#34;$http_user_agent&#34; &#34;$http_x_forwarded_for&#34;&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1"># access_log /var/log/nginx/access.log main;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="n">sendfile</span> <span class="n">on</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="c1">#tcp_nopush     on;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="n">keepalive_timeout</span> <span class="mi">65</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1"># 配置多个proxy_cache_path以便实现不同的业务</span>
</span></span><span class="line"><span class="cl">    <span class="n">proxy_connect_timeout</span> <span class="mi">10</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">proxy_read_timeout</span> <span class="mi">180</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">proxy_send_timeout</span> <span class="mi">5</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">proxy_buffer_size</span> <span class="mi">16</span><span class="n">k</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">proxy_buffers</span> <span class="mi">4</span> <span class="mi">32</span><span class="n">k</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">proxy_busy_buffers_size</span> <span class="mi">96</span><span class="n">k</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">proxy_temp_file_write_size</span> <span class="mi">96</span><span class="n">k</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">proxy_temp_path</span> <span class="o">/</span><span class="n">tmp</span><span class="o">/</span><span class="n">nginx</span><span class="o">/</span><span class="n">temp_dir</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">proxy_cache_path</span> <span class="o">/</span><span class="n">tmp</span><span class="o">/</span><span class="n">nginx</span><span class="o">/</span><span class="n">cache1m</span> <span class="n">levels</span><span class="o">=</span><span class="mi">1</span><span class="p">:</span><span class="mi">2</span> <span class="n">keys_zone</span><span class="o">=</span><span class="n">cache1m</span><span class="p">:</span><span class="mi">100</span><span class="n">m</span> <span class="n">inactive</span><span class="o">=</span><span class="mi">1</span><span class="n">m</span> <span class="n">max_size</span><span class="o">=</span><span class="mi">10</span><span class="n">g</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">proxy_cache_path</span> <span class="o">/</span><span class="n">tmp</span><span class="o">/</span><span class="n">nginx</span><span class="o">/</span><span class="n">cache5m</span> <span class="n">levels</span><span class="o">=</span><span class="mi">1</span><span class="p">:</span><span class="mi">2</span> <span class="n">keys_zone</span><span class="o">=</span><span class="n">cache5m</span><span class="p">:</span><span class="mi">100</span><span class="n">m</span> <span class="n">inactive</span><span class="o">=</span><span class="mi">5</span><span class="n">m</span> <span class="n">max_size</span><span class="o">=</span><span class="mi">10</span><span class="n">g</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">proxy_cache_path</span> <span class="o">/</span><span class="n">tmp</span><span class="o">/</span><span class="n">nginx</span><span class="o">/</span><span class="n">cache10m</span> <span class="n">levels</span><span class="o">=</span><span class="mi">1</span><span class="p">:</span><span class="mi">2</span> <span class="n">keys_zone</span><span class="o">=</span><span class="n">cache10m</span><span class="p">:</span><span class="mi">100</span><span class="n">m</span> <span class="n">inactive</span><span class="o">=</span><span class="mi">10</span><span class="n">m</span> <span class="n">max_size</span><span class="o">=</span><span class="mi">10</span><span class="n">g</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">proxy_cache_path</span> <span class="o">/</span><span class="n">tmp</span><span class="o">/</span><span class="n">nginx</span><span class="o">/</span><span class="n">cache30m</span> <span class="n">levels</span><span class="o">=</span><span class="mi">1</span><span class="p">:</span><span class="mi">2</span> <span class="n">keys_zone</span><span class="o">=</span><span class="n">cache30m</span><span class="p">:</span><span class="mi">100</span><span class="n">m</span> <span class="n">inactive</span><span class="o">=</span><span class="mi">30</span><span class="n">m</span> <span class="n">max_size</span><span class="o">=</span><span class="mi">10</span><span class="n">g</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">proxy_cache_path</span> <span class="o">/</span><span class="n">tmp</span><span class="o">/</span><span class="n">nginx</span><span class="o">/</span><span class="n">cache1h</span> <span class="n">levels</span><span class="o">=</span><span class="mi">1</span><span class="p">:</span><span class="mi">2</span> <span class="n">keys_zone</span><span class="o">=</span><span class="n">cache1h</span><span class="p">:</span><span class="mi">100</span><span class="n">m</span> <span class="n">inactive</span><span class="o">=</span><span class="mi">1</span><span class="n">h</span> <span class="n">max_size</span><span class="o">=</span><span class="mi">10</span><span class="n">g</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">proxy_cache_path</span> <span class="o">/</span><span class="n">tmp</span><span class="o">/</span><span class="n">nginx</span><span class="o">/</span><span class="n">cache12h</span> <span class="n">levels</span><span class="o">=</span><span class="mi">1</span><span class="p">:</span><span class="mi">2</span> <span class="n">keys_zone</span><span class="o">=</span><span class="n">cache12h</span><span class="p">:</span><span class="mi">100</span><span class="n">m</span> <span class="n">inactive</span><span class="o">=</span><span class="mi">12</span><span class="n">h</span> <span class="n">max_size</span><span class="o">=</span><span class="mi">10</span><span class="n">g</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">proxy_cache_path</span> <span class="o">/</span><span class="n">tmp</span><span class="o">/</span><span class="n">nginx</span><span class="o">/</span><span class="n">cache1d</span> <span class="n">levels</span><span class="o">=</span><span class="mi">1</span><span class="p">:</span><span class="mi">2</span> <span class="n">keys_zone</span><span class="o">=</span><span class="n">cache1d</span><span class="p">:</span><span class="mi">100</span><span class="n">m</span> <span class="n">inactive</span><span class="o">=</span><span class="mi">1</span><span class="n">d</span> <span class="n">max_size</span><span class="o">=</span><span class="mi">10</span><span class="n">g</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">proxy_cache_path</span> <span class="o">/</span><span class="n">tmp</span><span class="o">/</span><span class="n">nginx</span><span class="o">/</span><span class="n">cache7d</span> <span class="n">levels</span><span class="o">=</span><span class="mi">1</span><span class="p">:</span><span class="mi">2</span> <span class="n">keys_zone</span><span class="o">=</span><span class="n">cache7d</span><span class="p">:</span><span class="mi">100</span><span class="n">m</span> <span class="n">inactive</span><span class="o">=</span><span class="mi">7</span><span class="n">d</span> <span class="n">max_size</span><span class="o">=</span><span class="mi">10</span><span class="n">g</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">proxy_cache_path</span> <span class="o">/</span><span class="n">tmp</span><span class="o">/</span><span class="n">nginx</span><span class="o">/</span><span class="n">cache30d</span> <span class="n">levels</span><span class="o">=</span><span class="mi">1</span><span class="p">:</span><span class="mi">2</span> <span class="n">keys_zone</span><span class="o">=</span><span class="n">cache30d</span><span class="p">:</span><span class="mi">100</span><span class="n">m</span> <span class="n">inactive</span><span class="o">=</span><span class="mi">30</span><span class="n">d</span> <span class="n">max_size</span><span class="o">=</span><span class="mi">10</span><span class="n">g</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="n">server</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">listen</span> <span class="mi">80</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="n">server_name</span> <span class="o">.*</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="n">location</span> <span class="o">/</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">root</span> <span class="n">html</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="n">index</span> <span class="n">index</span><span class="o">.</span><span class="n">html</span> <span class="n">index</span><span class="o">.</span><span class="n">htm</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="n">error_page</span> <span class="mi">500</span> <span class="mi">502</span> <span class="mi">503</span> <span class="mi">504</span> <span class="o">/</span><span class="mi">50</span><span class="n">x</span><span class="o">.</span><span class="n">html</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="n">location</span> <span class="o">=</span> <span class="o">/</span><span class="mi">50</span><span class="n">x</span><span class="o">.</span><span class="n">html</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">root</span> <span class="n">html</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="n">location</span> <span class="o">/</span><span class="n">_proxy</span><span class="o">/</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="c1"># 跨域支持</span>
</span></span><span class="line"><span class="cl">            <span class="k">if</span> <span class="p">(</span><span class="o">$</span><span class="n">request_method</span> <span class="o">=</span> <span class="s1">&#39;OPTIONS&#39;</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">                <span class="n">add_header</span> <span class="s1">&#39;Access-Control-Allow-Origin&#39;</span> <span class="s1">&#39;*&#39;</span> <span class="n">always</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">                <span class="n">add_header</span> <span class="s1">&#39;Access-Control-Allow-Methods&#39;</span> <span class="s1">&#39;GET,POST,OPTIONS,PUT,DELETE&#39;</span> <span class="n">always</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">                <span class="n">add_header</span> <span class="s1">&#39;Access-Control-Allow-Headers&#39;</span> <span class="s1">&#39;*&#39;</span> <span class="n">always</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">                <span class="n">add_header</span> <span class="s1">&#39;Access-Control-Max-Age&#39;</span> <span class="mi">1728000</span> <span class="n">always</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">                <span class="n">add_header</span> <span class="s1">&#39;Content-Length&#39;</span> <span class="mi">0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">                <span class="n">add_header</span> <span class="s1">&#39;Content-Type&#39;</span> <span class="s1">&#39;text/plain; charset=utf-8&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">                <span class="k">return</span> <span class="mi">204</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">            <span class="k">if</span> <span class="p">(</span><span class="o">$</span><span class="n">request_method</span> <span class="o">~*</span> <span class="s1">&#39;(GET|POST|DELETE|PUT)&#39;</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">                <span class="n">add_header</span> <span class="s1">&#39;Access-Control-Allow-Origin&#39;</span> <span class="s1">&#39;*&#39;</span> <span class="n">always</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">            <span class="c1"># 解析缓存域</span>
</span></span><span class="line"><span class="cl">            <span class="n">set</span> <span class="o">$</span><span class="n">proxy_cache</span> <span class="s2">&#34;cache1m&#34;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="k">if</span> <span class="p">(</span> <span class="o">$</span><span class="n">request_uri</span> <span class="o">~</span> <span class="s2">&#34;/_proxy/(.+)\?&#34;</span> <span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">                <span class="n">set</span> <span class="o">$</span><span class="n">proxy_cache</span> <span class="o">$</span><span class="mi">1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="p">}</span>
</span></span><span class="line"><span class="cl">            <span class="c1"># 不存在的缓存域时返回404异常</span>
</span></span><span class="line"><span class="cl">            <span class="k">if</span> <span class="p">(</span><span class="o">!-</span><span class="n">e</span> <span class="o">/</span><span class="n">tmp</span><span class="o">/</span><span class="n">nginx</span><span class="o">/$</span><span class="n">proxy_cache</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">                <span class="n">add_header</span> <span class="n">proxy_cache_time</span> <span class="s1">&#39;cache dir is not found&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">                <span class="k">return</span> <span class="mi">404</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="p">}</span>
</span></span><span class="line"><span class="cl">            <span class="c1"># 缓存配置</span>
</span></span><span class="line"><span class="cl">            <span class="n">proxy_cache</span> <span class="o">$</span><span class="n">proxy_cache</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="c1"># proxy_cache_valid 200 302 24h;</span>
</span></span><span class="line"><span class="cl">            <span class="c1"># proxy_cache_valid 301 30d;</span>
</span></span><span class="line"><span class="cl">            <span class="n">proxy_cache_valid</span> <span class="n">any</span> <span class="mi">30</span><span class="n">d</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="n">expires</span> <span class="mi">30</span><span class="n">d</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="c1"># 增加响应头记录是否命中缓存</span>
</span></span><span class="line"><span class="cl">            <span class="n">add_header</span> <span class="n">proxy_cache_time</span> <span class="o">$</span><span class="n">proxy_cache</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="n">add_header</span> <span class="n">proxy_cache_status</span> <span class="o">$</span><span class="n">upstream_cache_status</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="c1"># 关闭重定向</span>
</span></span><span class="line"><span class="cl">            <span class="n">proxy_redirect</span> <span class="n">off</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="c1"># 能解析转发 URL 的 DNS</span>
</span></span><span class="line"><span class="cl">            <span class="n">resolver</span> <span class="mf">223.5</span><span class="o">.</span><span class="mf">5.5</span> <span class="mf">8.8</span><span class="o">.</span><span class="mf">8.8</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="c1"># 获取真实url</span>
</span></span><span class="line"><span class="cl">            <span class="k">if</span> <span class="p">(</span><span class="o">$</span><span class="n">query_string</span> <span class="o">~</span> <span class="s2">&#34;^url=(.+)&#34;</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">                <span class="n">set</span> <span class="o">$</span><span class="n">proxy_url</span> <span class="o">$</span><span class="mi">1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="p">}</span>
</span></span><span class="line"><span class="cl">            <span class="c1"># 获取 host</span>
</span></span><span class="line"><span class="cl">            <span class="n">set</span> <span class="o">$</span><span class="n">proxy_host</span> <span class="o">$</span><span class="n">host</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="k">if</span> <span class="p">(</span><span class="o">$</span><span class="n">proxy_url</span> <span class="o">~</span> <span class="s2">&#34;https?://(.*?)/&#34;</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">                <span class="n">set</span> <span class="o">$</span><span class="n">proxy_host</span> <span class="o">$</span><span class="mi">1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="p">}</span>
</span></span><span class="line"><span class="cl">            <span class="n">proxy_set_header</span> <span class="n">Host</span> <span class="o">$</span><span class="n">proxy_host</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="n">proxy_set_header</span> <span class="n">X</span><span class="o">-</span><span class="n">Forwarded</span><span class="o">-</span><span class="n">For</span> <span class="o">$</span><span class="n">remote_addr</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="n">proxy_pass</span> <span class="o">$</span><span class="n">proxy_url</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p><strong>docker-compose.yml 快速测试：</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-gdscript3" data-lang="gdscript3"><span class="line"><span class="cl"><span class="n">version</span><span class="p">:</span> <span class="s2">&#34;3.6&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">services</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">  <span class="n">nginx</span><span class="o">-</span><span class="n">cache</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">    <span class="n">image</span><span class="p">:</span> <span class="n">nginx</span><span class="p">:</span><span class="mf">1.20</span><span class="o">.</span><span class="mi">2</span><span class="o">-</span><span class="n">alpine</span>
</span></span><span class="line"><span class="cl">    <span class="n">container_name</span><span class="p">:</span> <span class="n">nginx</span><span class="o">-</span><span class="n">cache</span>
</span></span><span class="line"><span class="cl">    <span class="n">restart</span><span class="p">:</span> <span class="n">always</span>
</span></span><span class="line"><span class="cl">    <span class="n">network_mode</span><span class="p">:</span> <span class="n">bridge</span>
</span></span><span class="line"><span class="cl">    <span class="n">ports</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">    <span class="o">-</span> <span class="s2">&#34;8000:80&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="n">environment</span><span class="p">:</span>
</span></span><span class="line"><span class="cl">    <span class="o">-</span> <span class="n">TZ</span><span class="o">=</span><span class="n">Asia</span><span class="o">/</span><span class="n">Shanghai</span>
</span></span><span class="line"><span class="cl">    <span class="n">volumes</span><span class="p">:</span>  
</span></span><span class="line"><span class="cl">    <span class="o">-</span> <span class="s2">&#34;./nginx.conf:/etc/nginx/nginx.conf:ro&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="o">-</span> <span class="s2">&#34;./logs:/var/log/nginx:rw&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="o">-</span> <span class="s2">&#34;./temp:/tmp/nginx&#34;</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>参考链接：</p>
<ol>
<li><a href="https://giphy.com/search/Tom-and-Jerry?utm_source=wechat_session&amp;utm_medium=social&amp;from=singlemessage" target="_blank" rel="noopener noreffer ">Nginx 缓存使用官方指南</a></li>
<li><a href="https://docs.nginx.com/nginx/admin-guide/content-cache/content-caching/" target="_blank" rel="noopener noreffer ">Nginx 内容缓存文档</a></li>
</ol>
</div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>Updated on 2021-12-21</span>
            </div></div>
        <div class="post-info-line">
            <div class="post-info-md"></div>
            <div class="post-info-share">
                <span><a href="javascript:void(0);" title="Share on Twitter" data-sharer="twitter" data-url="https://100180.xyz/posts/2021122101/" data-title="Nginx 内容缓存及常见参数配置" data-hashtags="nginx"><i class="fab fa-twitter fa-fw" aria-hidden="true"></i></a><a href="javascript:void(0);" title="Share on Facebook" data-sharer="facebook" data-url="https://100180.xyz/posts/2021122101/" data-hashtag="nginx"><i class="fab fa-facebook-square fa-fw" aria-hidden="true"></i></a><a href="javascript:void(0);" title="Share on Linkedin" data-sharer="linkedin" data-url="https://100180.xyz/posts/2021122101/"><i class="fab fa-linkedin fa-fw" aria-hidden="true"></i></a><a href="javascript:void(0);" title="Share on 微博" data-sharer="weibo" data-url="https://100180.xyz/posts/2021122101/" data-title="Nginx 内容缓存及常见参数配置"><i class="fab fa-weibo fa-fw" aria-hidden="true"></i></a><a href="javascript:void(0);" title="Share on 百度" data-sharer="baidu" data-url="https://100180.xyz/posts/2021122101/" data-title="Nginx 内容缓存及常见参数配置"><i data-svg-src="/lib/simple-icons/icons/baidu.min.svg" aria-hidden="true"></i></a><a href="javascript:void(0);" title="Share on Evernote" data-sharer="evernote" data-url="https://100180.xyz/posts/2021122101/" data-title="Nginx 内容缓存及常见参数配置"><i class="fab fa-evernote fa-fw" aria-hidden="true"></i></a><a href="javascript:void(0);" title="Share on Skype" data-sharer="skype" data-url="https://100180.xyz/posts/2021122101/" data-title="Nginx 内容缓存及常见参数配置"><i class="fab fa-skype fa-fw" aria-hidden="true"></i></a></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw" aria-hidden="true"></i>&nbsp;<a href="/tags/nginx/">Nginx</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">Back</a></span>&nbsp;|&nbsp;<span><a href="/">Home</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="/posts/2021111601/" class="prev" rel="prev" title="深度分析 IT 职业发展路线，做一个透彻的打工人"><i class="fas fa-angle-left fa-fw" aria-hidden="true"></i>深度分析 IT 职业发展路线，做一个透彻的打工人</a>
            <a href="/posts/2022083001/" class="next" rel="next" title="Mac中Git忽略.DS_Store文件">Mac中Git忽略.DS_Store文件<i class="fas fa-angle-right fa-fw" aria-hidden="true"></i></a></div>
</div>
</article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line" itemscope itemtype="http://schema.org/CreativeWork"><i class="far fa-copyright fa-fw" aria-hidden="true"></i><span itemprop="copyrightYear">2017 - 2025</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">MidAug</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a></span><span class="icp-splitter">&nbsp;|&nbsp;</span><br class="icp-br"/>
                    <span class="icp"><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7196301099178927" crossorigin="anonymous"></script> <a target=_blank href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=31011502019261"><img style="vertical-align: text-bottom; height: 1rem" class="lazyload" data-src="/beian/gongan.png"><span>沪公网安备 31011502019261号</span></a><span class="icp-splitter">&nbsp;|&nbsp;</span><a href="https://beian.miit.gov.cn/" target="_blank">沪ICP备2022026688号-1</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="Back to Top">
                <i class="fas fa-arrow-up fa-fw" aria-hidden="true"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="View Comments">
                <i class="fas fa-comment fa-fw" aria-hidden="true"></i>
            </a>
        </div><script type="text/javascript" src="/lib/autocomplete/autocomplete.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.min.js"></script><script type="text/javascript" src="/lib/lazysizes/lazysizes.min.js"></script><script type="text/javascript" src="/lib/clipboard/clipboard.min.js"></script><script type="text/javascript" src="/lib/sharer/sharer.min.js"></script><script type="text/javascript" src="/lib/typeit/index.umd.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"Copy to clipboard","maxShownLines":50},"comment":{},"data":{"id-1":"八月半的研发杂谈","id-2":"八月半的研发杂谈"},"search":{"highlightTag":"em","lunrIndexURL":"/index.json","maxResultLength":10,"noResultsFound":"No results found","snippetLength":80,"type":"lunr"},"typeit":{"cursorChar":"|","cursorSpeed":1000,"data":{"id-1":["id-1"],"id-2":["id-2"]},"duration":-1,"speed":30}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
